package com.liu.service.impl;

import com.liu.controller.dto.DepDTO;
import com.liu.controller.dto.HosDepDTO;
import com.liu.controller.dto.PasswordDTO;
import com.liu.entity.Physician;
import com.liu.mapper.PhysicianMapper;
import com.liu.service.PhysicianService;
import com.liu.utils.TokenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Physician)表服务实现类
 *
 * @author makejava
 * @since 2023-02-11 13:34:03
 */
@Service("physicianServiceImpl")
@Transactional
public class PhysicianServiceImpl implements PhysicianService {
    @Resource
    private PhysicianMapper physicianMapper;


    @Override
    public List<Physician> findAllPhy(String nickname, String identity, String grade, String name) {
        return this.physicianMapper.findAllPhy(nickname, identity, grade, name);
    }

    @Override
    public Long allCount(String nickname, String identity, String grade, String name) {
        return this.physicianMapper.allCount(nickname, identity, grade, name);
    }

    @Override
    public List<DepDTO> findPhyCount(Integer id) {
        return this.physicianMapper.findPhyCount(id);
    }

    @Override
    public List<Physician> queryAllPhyStage(Integer pageNum,Integer pageSize,Integer hosId,Integer depId) {
        return this.physicianMapper.queryAllPhyStage(pageNum, pageSize, hosId,depId);
    }

    @Override
    public Physician queryById(Integer id) {
        return this.physicianMapper.queryById(id);
    }

    @Override
    public List<Physician> findAll() {
        return this.physicianMapper.findAll();
    }

    @Override
    public List<Physician> selectPhyByDep(String name) {
        return this.physicianMapper.selectPhyByDep(name);
    }

    @Override
    public List<Physician> findPhyByDepId(Integer id) {
        return this.physicianMapper.findPhyByDepId(id);
    }

    @Override
    public Long phyCount(HosDepDTO hosDepDTO) {
        return this.physicianMapper.phyCount(hosDepDTO);
    }

    @Override
    public List<Physician> findPhy(HosDepDTO hosDepDTO) {
        return this.physicianMapper.findPhy(hosDepDTO);
    }

    @Override
    public List<Physician> queryAllByLimit(Integer pageNum, Integer pageSize, String username, String identity) {
        return this.physicianMapper.queryAllByLimit(pageNum,pageSize,username,identity);
    }

    @Override
    public long count(String username, String identity) {
        return this.physicianMapper.count(username,identity);
    }

    @Override
    public int insert(Physician physician) {
        return this.physicianMapper.insert(physician);
    }

    @Override
    public int insertBatch(List<Physician> entities) {
        return this.physicianMapper.insertBatch(entities);
    }

    @Override
    public int update(Physician physician) {
        return this.physicianMapper.update(physician);
    }

    @Override
    public int updatePwd(PasswordDTO passwordDTO) {
        return this.physicianMapper.updatePwd(passwordDTO);
    }

    @Override
    public int deleteById(Integer id) {
        return this.physicianMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return this.physicianMapper.deleteByIds(ids);
    }

    @Override
    public Physician login(Map<String, Object> map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        Physician physician = this.physicianMapper.findByName(username);
        if(physician == null) throw new RuntimeException("用户名输入错误，请重新输入");
        if (!physician.getPassword().equals(password)) throw new RuntimeException("密码输入错误！");
        String token = TokenUtils.genToken(physician.getId().toString(), physician.getPassword());
        physician.setToken(token);
        return physician;
    }

    @Override
    public Physician findByName(String username) {
        return this.physicianMapper.findByName(username);
    }
}
